package com.jy.unkown.page;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

import com.amjy.dsl.R;
import com.jy.common.base.BaseDialog;
import com.jy.unkown.AD.BaseUnkownAd;
import com.jy.unkown.view.UnkownADView;
import com.jy.unkown.view.UnkownInterstitialView;
import com.jy.utils.call.NoDoubleClick;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class InterstitalDialog extends BaseDialog {
    Activity mActivity;

    public InterstitalDialog(@NonNull Activity activity) {
        super(activity);
        this.mActivity = activity;
    }

    BaseUnkownAd baseUnkownAd;
    UnkownInterstitialView unkownADView;

    public void setUnkownAD(BaseUnkownAd unkownAD) {
        this.baseUnkownAd = unkownAD;
    }
    View closeBtn;
    @Override
    public void initUI() {

        Observable.timer(3000, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        closeBtn.setOnClickListener(new NoDoubleClick() {
                            @Override
                            public void noDoubleCall(View view) {
                                dismiss();
                                unkownADView.adClose();
                            }
                        });
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });


        FrameLayout adRL = findViewById(R.id.adRL);
        try {
            unkownADView = new UnkownInterstitialView(mActivity);
            unkownADView.setAdCallBack(baseUnkownAd.getAdCallBack());
            unkownADView.setUnkownAd(baseUnkownAd.getUnkownAdBean());
            unkownADView.setVideoMute(baseUnkownAd.isVideoMute());
        } catch (Exception e) {
            e.printStackTrace();
        }
        adRL.addView(unkownADView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        unkownADView.show();
        closeBtn = unkownADView.findViewById(R.id.closeBtn);
    }

    @Override
    protected void doAfterShow() {
        super.doAfterShow();

    }

    @Override
    public void onActivityResumed(Activity activity) {
        super.onActivityResumed(activity);
        if (mActivity == activity) {
            if (unkownADView != null) {
                unkownADView.resume();
            }
        }

    }

    @Override
    public void onActivityPaused(Activity activity) {
        super.onActivityPaused(activity);
        if (mActivity == activity) {
            if (unkownADView != null) {
                unkownADView.onPause();
            }
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (unkownADView != null) {
            unkownADView.onDestory();
        }

    }

    @Override
    public int layoutId() {
        return R.layout.interstital_dialog;
    }
}
